
package com.wyzz.hr.action;

import java.util.Date;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpJobHisBo;
import com.wyzz.hr.pojo.Emphistoryjob;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.spring.SpringBeanFactory;

public class EmpJobHisDWR extends BaseAction {

	public EmpJobHisDWR() {
	}

	public String jobHisAdd(Emphistoryjob emphistoryjob, String empId) {
		String auth = DWRUtil.checkAuth("empjobhisdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		if ("self".equals(empId))
			empId = getCurrentEmpNo();
		if ("SUB".equals(auth) && !checkAuth(empId)) {
			return "noauth";
		} else {
			empJobHisBo = (IEmpJobHisBo) SpringBeanFactory
					.getBean("empJobHisBo");
			emphistoryjob.setEmployee(new Employee(empId));
			emphistoryjob.setEhjCreateBy(getCurrentEmpNo());
			emphistoryjob.setEhjCreateDate(new Date());
			emphistoryjob.setEhjLastChangeBy(getCurrentEmpNo());
			emphistoryjob.setEhjLastChangeTime(new Date());
			String ejhId = empJobHisBo.insert(emphistoryjob);
			return ejhId;
		}
	}

	public String jobHisDel(String ehjId) {
		String auth = DWRUtil.checkAuth("empjobhisdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empJobHisBo = (IEmpJobHisBo) SpringBeanFactory.getBean("empJobHisBo");
		String fetches[] = { Emphistoryjob.PROP_EMPLOYEE };
		Emphistoryjob emphj = empJobHisBo.load(ehjId, fetches);
		if ("SUB".equals(auth) && !checkAuth(emphj.getEmployee().getId())) {
			return "noauth";
		} else {
			empJobHisBo.delete(emphj);
			return "success";
		}
	}

	public String jobHisUpdate(Emphistoryjob emphistoryjob) {
		String auth = DWRUtil.checkAuth("empjobhisdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empJobHisBo = (IEmpJobHisBo) SpringBeanFactory.getBean("empJobHisBo");
		String fetches[] = { Emphistoryjob.PROP_EMPLOYEE };
		Emphistoryjob empOldHJ = empJobHisBo.load(emphistoryjob.getEhjId(),
				fetches);
		if (empOldHJ == null)
			return "noId";
		if ("SUB".equals(auth) && !checkAuth(empOldHJ.getEmployee().getId())) {
			return "noauth";
		} else {
			empOldHJ.setEhjCompany(emphistoryjob.getEhjCompany());
			empOldHJ.setEhjDateStart(emphistoryjob.getEhjDateStart());
			empOldHJ.setEhjDateEnd(emphistoryjob.getEhjDateEnd());
			empOldHJ.setEhjPosition(emphistoryjob.getEhjPosition());
			empOldHJ.setEhjDescription(emphistoryjob.getEhjDescription());
			emphistoryjob.setEhjLastChangeBy(getCurrentEmpNo());
			emphistoryjob.setEhjLastChangeTime(new Date());
			empJobHisBo.update(empOldHJ);
			return "success";
		}
	}

	public Emphistoryjob loadJobHis(String ehjId) {
		if (StringUtils.isEmpty(ehjId)) {
			return null;
		} else {
			ehjId = ehjId.trim();
			empJobHisBo = (IEmpJobHisBo) SpringBeanFactory
					.getBean("empJobHisBo");
			Emphistoryjob result = empJobHisBo.load(ehjId, new String[0]);
			return result;
		}
	}

	private static final long serialVersionUID = 1L;
	IEmpJobHisBo empJobHisBo;
}
